fix(postgres): transpile DAY/MONTH/YEAR to EXTRACT [CLAUDE]#7798
Open
vjpovlitz wants to merge 1 commit into
Open
fix(postgres): transpile DAY/MONTH/YEAR to EXTRACT [CLAUDE]#7798vjpovlitz wants to merge 1 commit into
vjpovlitz wants to merge 1 commit into
Conversation
Collaborator
|
@vjpovlitz thanks for the PR, in order to transpile For example the input (tsql) can be a string value, which we can't transpile in postgres as is (we need on the parsing side of tsql to wrap the function argument with a Let's first make an analysis on all of these cases/inputs ^ in order to know the exact semantics of these functions, then we can come up with a robust implementation that covers theses cases (or a part of them). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PostgreSQL has no scalar
DAY/MONTH/YEARfunctions — date parts are read viaEXTRACT(field FROM source). When transpiling from a dialect that does have them (e.g.T-SQL), SQLGlot emitted the functions unchanged, producing invalid Postgres SQL.
This adds Postgres generator rules so
exp.Day/exp.Month/exp.Yearrender asEXTRACT(<part> FROM ...).Reproduction
MONTHandYEARbehave the same.EXTRACTis the standard Postgres construct for theseparts: https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
Approach
exp.Day/exp.Month/exp.Yearnodes, so only the Postgres output was wrong. Parsing and otherdialects are untouched (
tsql -> tsqlstill yieldsDAY(d)).exp.Extractnode rather than hand-built SQL, and the threerules share a small
_extract_sql(part)factory in the style of the neighboring_date_add_sql(kind)helper.they likewise lack scalar
DAY/MONTH/YEAR.Test
tests/dialects/test_postgres.py::TestPostgres::test_extract_date_partsasserts theT-SQL -> Postgres transpilation and that the
EXTRACTform round-trips in Postgres.Fixes #6220